﻿using System;
using System.Collections.Generic;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;

namespace Game.Common
{
    public class GameHexagon
    {
        readonly static double _Shift = 0.28;
        
        public static double Shift
        {
            get { return _Shift; }
        }
        
        public static Polygon GetPolygon(Size size, 
                                         double scale = 1)
        {
            double shift1 = Shift;
            Point[] gamePoints = new Point[]
            {
                new Point( shift1, 0.00),
                new Point( 1 - shift1, 0.00),
                new Point( 1 + shift1, 0.50),
                new Point( 1 - shift1, 1.00),
                new Point( shift1, 1.00),
                new Point(-shift1, 0.50),
                new Point( shift1, 0.00)
            };
            
            Point[] scaledPoints = ScalePoints(
                gamePoints, size, scale);
            
            Polygon polygon = new Polygon();
            
            PointCollection points = new PointCollection();
            foreach(Point p in scaledPoints)
            {
                points.Add(new Point(p.X, p.Y));
            }
            polygon.Points = points;
            return polygon;
        }

        public static Point[] GetPolygonPoints(Size size)
        {
            double shift1 = Shift;
            Point[] gamePoints = new Point[]
            {
                new Point( shift1, 0.00),
                new Point( 1 - shift1, 0.00),
                new Point( 1 + shift1, 0.50),
                new Point( 1 - shift1, 1.00),
                new Point( shift1, 1.00),
                new Point(-shift1, 0.50),
                new Point( shift1, 0.00)
            };

            return ScalePoints(gamePoints, size, 1);
        }

        public static Point[] ScalePoints(Point[] points,
                                          Size size,
                                          double scale)
        {
            double scaledWidth = (size.Width * scale);
            double scaledHeight = (size.Height * scale);
            
            int index = 0;
            
            Point[] scaledPoints = new Point[points.Length];
            foreach (Point p in points) 
            {
                float x = (float)(p.X * scaledWidth);
                float y = (float)(p.Y * scaledHeight);
                scaledPoints[index] = new Point(x, y);
                index++;
            }
            
            return scaledPoints;
        }
        
    }

}
